package demo02;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Demo02LambdaComparator {
    public static void main(String[] args) {
        ArrayList<Person> arrayList = new ArrayList<>();
        Collections.addAll(arrayList, new Person("土司鸡", 18), new Person("老虎", 20), new Person("粹花", 1));

        // 匿名内部类：
        Collections.sort(arrayList, new Comparator<Person>() {
        @Override
        public int compare(Person o1, Person o2) {
        return o1.getAge() - o2.getAge();
        }
        });

        // Lambda标准格式：
        Collections.sort(arrayList, (Person o1, Person o2) -> {
            return o1.getAge() - o2.getAge();
        });

        // Lambda省略格式：
        Collections.sort(arrayList, (o1, o2) -> o1.getAge() - o2.getAge());

        for (Person person : arrayList) {
            System.out.println(person);
        }
        /*
         *  输出结果：
         *  Person [age=1, name=粹花]
         *  Person [age=18, name=土司鸡]
         *  Person [age=20, name=老虎]
         */
    }
}
